package org.xiaom.butler.service;

import java.util.ArrayList;
import java.util.List;

import org.xiaom.butler.bean.Content;
import org.xiaom.butler.bean.Department;
import org.xiaom.butler.bean.Hotel;
import org.xiaom.butler.util.AppHelper;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;

public class HotelService extends BeanDataService<Hotel> {

	public HotelService(Context context) {
		super(context);
	}

	@Override
	public void insert(List<Hotel> beans) {

		for (Hotel bean : beans) {
			ContentValues cv=splitBean(bean);
			this.db.insert(Hotel.table_name, null, cv);
		}
	}

	@Override
	public void update(List<Hotel> beans) {
		for (Hotel bean : beans) {
			ContentValues cv =splitBean(bean);
			this.db.update(Hotel.table_name, cv,
					Hotel.id_ + "=" + bean.getId(), null);
		}
	}

	@Override
	public void delete(String[] ids) {
		for (String id : ids) {
			this.db.delete(Hotel.table_name, Hotel.id_ + "=" + id, null);
		}

	}
	String[] cols=new String[]{
			Hotel.id_,
			Hotel.name_,
			Hotel.city_number,
			Hotel.image_path,
			Hotel.phone_,
			Hotel.version_
			};
	@Override
	public List<Hotel> get(String[] ids) {
		List<Hotel> list=new ArrayList<Hotel>();
		Cursor c;
		if(ids==null){
			c=db.query(Hotel.table_name, cols, null,null, null, null, null);
			if(c.getCount()>0){
				while(c.moveToNext()){
					Hotel bean=parseBean(c);
					list.add(bean);
				}
			}
			c.close();
		}else{
			for(String id:ids){
				c=db.query(Hotel.table_name, cols, Hotel.id_+"=?", new String[]{id}, null, null, null);
				if(c.getCount()==1){
					c.moveToFirst();
					Hotel bean=parseBean(c);
					list.add(bean);
				}
			}
		}
		return list;
	}

	@Override
	public void deleteAll() {
		db.execSQL("delete from "+Content.table_name);
		db.execSQL("delete from "+Department.table_name);
		db.execSQL("delete from "+Hotel.table_name);

	}

	@Override
	Hotel parseBean(Cursor c) {
		Hotel h = new Hotel();
		h.setId(Integer.parseInt(c.getString(0)));
		h.setName(c.getString(1));
		h.setCityNumber(c.getString(2));
		h.setImagePath(c.getString(3));
		h.setPhone(c.getString(4));
		h.setVersion(c.getInt(5));
		return h;
	}

	@Override
	ContentValues splitBean(Hotel bean) {
		ContentValues cv = new ContentValues();

		cv.put(Hotel.id_, bean.getId());
		cv.put(Hotel.name_, bean.getName());
		cv.put(Hotel.city_number, bean.getCityNumber());
		cv.put(Hotel.image_path, bean.getImagePath());
		cv.put(Hotel.phone_, bean.getPhone());
		cv.put(Hotel.version_, bean.getVersion());
		return cv;
	}
	public Hotel getCurrentHotel(){
		Hotel h=null;
		SharedPreferences sp=context.getSharedPreferences(AppHelper.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		String[] ids=new String[]{sp.getString(AppHelper.SP_CURRENT_HOTEL, "1")};
		List<Hotel> hotels=get(ids);
		if(hotels.size()>0){
			h=hotels.get(0);
		}
		return h;
	}

}
